import Lable from "./Lable";
import Sprite from "./Sprite";

const _gm = require('gm').subClass({ imageMagick: true })
let imgPath = 'D:/v5/bot/bot_-readme/resources';
const outDir = 'D:/v5/bot/bot_-readme/output';

export default class Node {
    private image: any;
    private width: number;
    private height: number;
    private child: any[] = []
    constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
        // this.image = _gm(width, height, "none")
        this.image = _gm(width, height, "#ffffff")
    }
    /**
     * 增加图片元素
     */
    addSprite() {
        let item = new Sprite(this);
        this.child.push(item);
        return item;
    }
    /**
     * 增加文字
     */
    addLable() {
        let item = new Lable(this);
        this.child.push(item);
        return item;
    }
    render(offsetX: number, offsetY: number, child: Sprite[] | Lable[]) {
        let image = this.image;
        for (let index = 0; index < child.length; index++) {
            const item = child[index];
            let x = offsetX + item.x;
            let y = offsetY + item.y;
            if (item instanceof Sprite) {
                image.draw(`image Over ${x}, ${y}, ${item.width}, ${item.height} "${imgPath}/${item.path}"`);
            }
            if (item instanceof Lable) {
                // TODO:优化
                image.font(`${imgPath}/font/${item.font}`)
                image.fontSize(item.fontSize)
                image.fill(item.color)
                // image.stroke("#000000", 1)
                image.drawText(x, y, item.string)

   
            }
            if (item.child.length) {
                item.child.forEach(e => {
                    e.draw();
                });
            }
        }
    }
    write():Promise<string> {
        let startTime = Date.now();
        let image = this.image;
        let outId = Date.now();
        for (let index = 0; index < this.child.length; index++) {
            const item = this.child[index];
            this.render(0, 0, [item])
        }
        let _outDir = `${outDir}/${outId}.png`
        return new Promise((resolve,reject)=>{
            image.write(_outDir, async (err) => {
                if (err) {
                    console.log(err);
                }
                console.info(`write time:${(Date.now() - startTime) / 1000} / s`)
                console.log('绘制完成', _outDir)
                await new Promise(rs => { setTimeout(rs, 1000) });
                resolve(_outDir)
            });
        })
      
    }
    test() {
        let image = this.image;

        image.draw(`image Over 38, 25, 250, 300 "${imgPath}/test2.png"`);
        // image.font(`${imgPath}/font/ttf_cn.ttf`)
        // image.fontSize(22)
        // image.fill("#000000")
        // image.drawText(412, 264, `物理攻击:10000`)
        let outId = Date.now()
        let _outDir = `${outDir}/${outId}.png`
        image.write(_outDir, (err) => {
            if (err) {
                console.log(err);
            }
            console.log('绘制完成', _outDir)
            // resolve(outDir)
        });
    }
}
